﻿using Fiddler;
using FiddlerExUtils.Helper;
using FiddlerExUtils.Properties;
using FiddlerExUtils.Tools;
using mshtml;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FiddlerExUtils.Components
{
    public partial class WebBrowserComponent : WebBrowser
    {
        public WebBrowserComponent()
        {
            InitializeComponent();
        }

        public WebBrowserComponent(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
        }

        private void WebBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.C)
            {
                this.WebBrowserShortcutsEnabled = false;
                IHTMLDocument2 htmlDocument = this.Document.DomDocument as IHTMLDocument2;
                IHTMLSelectionObject currentSelection = htmlDocument.selection;
                if (currentSelection != null)
                {
                    IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                    if (range != null && range.text != null)
                    {
                        Utilities.CopyToClipboard(range.text);
                    }
                }
            }
            else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
            {
                this.WebBrowserShortcutsEnabled = false;
                this.Document.ExecCommand("SelectAll", true, null);
            }
            else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.F)
            {
                this.WebBrowserShortcutsEnabled = true;
            }
            else if (e.Modifiers == Keys.Control && e.KeyCode == Keys.D) //Debug html
            {
#if DEBUG
                this.IsWebBrowserContextMenuEnabled = true;
                this.WebBrowserShortcutsEnabled = true; 
#endif
            }
        }

        private void ContextMenuStrip2_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            this.WebBrowserShortcutsEnabled = false;
            IHTMLDocument2 htmlDocument = this.Document.DomDocument as IHTMLDocument2;
            IHTMLSelectionObject currentSelection = htmlDocument.selection;
            if (currentSelection != null)
            {
                IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                if (range != null && range.text != null)
                {
                    TSMI_CopyText.Enabled = true;
                    TSMI_SendTextToTextWizard.Enabled = true;
                }
                else
                {
                    TSMI_CopyText.Enabled = false;
                    TSMI_SendTextToTextWizard.Enabled = false;
                }
            }
        }

        private void TSMI_CopyText_Click(object sender, EventArgs e)
        {
            this.WebBrowserShortcutsEnabled = false;
            IHTMLDocument2 htmlDocument = this.Document.DomDocument as IHTMLDocument2;
            IHTMLSelectionObject currentSelection = htmlDocument.selection;
            if (currentSelection != null)
            {
                IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                if (range != null && range.text != null)
                {
                    Utilities.CopyToClipboard(range.text);
                }
            }
        }

        private void TSMI_SelectAll_Click(object sender, EventArgs e)
        {
            this.WebBrowserShortcutsEnabled = false;
            this.Document.ExecCommand("SelectAll", true, null);
        }

        /// <summary>
        /// 导入模拟键盘的方法
        /// </summary>
        /// <param name="bVk" >按键的虚拟键值</param>
        /// <param name= "bScan" >扫描码，一般不用设置，用0代替就行</param>
        /// <param name= "dwFlags" >选项标志：0：表示按下，2：表示松开</param>
        /// <param name= "dwExtraInfo">一般设置为0</param>
        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);

        private void TSMI_FindText_Click(object sender, EventArgs e)
        {
            this.Focus();
            this.WebBrowserShortcutsEnabled = false;
            //SendKeys.Send("^{A}");
            keybd_event(Keys.ControlKey, 0, 0, 0);
            keybd_event(Keys.F, 0, 0, 0);
            keybd_event(Keys.ControlKey, 0, 2, 0);
            keybd_event(Keys.F, 0, 2, 0);
        }

        /// <summary>
        /// 发送节点Key+Value至TextWziard
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TSMI_SendTextToTextWizard_Click(object sender, EventArgs e)
        {
            this.WebBrowserShortcutsEnabled = false;
            IHTMLDocument2 htmlDocument = this.Document.DomDocument as IHTMLDocument2;
            IHTMLSelectionObject currentSelection = htmlDocument.selection;
            if (currentSelection != null)
            {
                IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
                if (range != null && range.text != null)
                {
                    FiddlerApplication.UI.actShowTextWizard(range.text);
                }
            }
        }

        private void PB_DecryptOrEncrypt_Click(object sender, EventArgs e)
        {

        }

        private void PB_CopyBody_Click(object sender, EventArgs e)
        {
            TSMI_SelectAll_Click(null, null);
            TSMI_CopyText_Click(null, null);
            this.Document.ExecCommand("Unselect", true, null);
        }

        private void PB_CopyBody_MouseEnter(object sender, EventArgs e)
        {
            PB_CopyBody.BackColor = Color.LightGray;
        }

        private void PB_CopyBody_MouseLeave(object sender, EventArgs e)
        {
            PB_CopyBody.BackColor = SystemColors.Control;
        }

    }
}
